<?php
/**
 * Created by PhpStorm.
 * User: Thanh Lâm
 * Date: 4/8/14
 * Time: 9:13 AM
 */
namespace Apps\Frontend\Forms;

use Phalcon\Forms\Form;
use Phalcon\Forms\Element\Text;
use Phalcon\Forms\Element\Password;
use Phalcon\Forms\Element\Submit;
use Phalcon\Forms\Element\Check;
use Phalcon\Forms\Element\Hidden;
use Phalcon\Validation\Validator\PresenceOf;
use Phalcon\Validation\Validator\Email;
use Phalcon\Validation\Validator\Identical;

class LoginForm extends Form{

    public function initialize(){
        $email = new Text('username', array(
            'placeholder' => 'Username',
            'class'       => 'form-control',
        ));
        $email->addValidators(array(
            new PresenceOf(array(
                'message' => 'Username không được rỗng'
            )),
        ));
        $this->add($email);
        // Password
        $password = new Password('password', array(
            'placeholder' => 'Password',
            'class'       => 'form-control',
        ));
        $password->addValidator(new PresenceOf(array(
            'message' => 'The password is required'
        )));
        $this->add($password);
        $csrf = new Hidden('csrf');

        $csrf->addValidator(new Identical(array(
            'value' => $this->security->getSessionToken(),
            'message' => 'Token hết hạn'
        )));
        $this->add($csrf);
        $this->add(new Submit('Login', array(
            'class' => 'btn btn-lg btn-primary btn-block'
        )));
    }
    /**
     * Prints messages for a specific element
     */
    public function messages($name){
        if ($this->hasMessagesFor($name)) {
            foreach ($this->getMessagesFor($name) as $message) {
                $this->flash->error($message);
            }
        }
    }
}
